package com.xyht.sca_s.student_manage_system.modules.repairCheck.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.repairCheck.entity.vo.*;
import com.xyht.sca_s.student_manage_system.modules.repairCheck.service.SmsRepairCheckApplyService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-06-19
 */
@SecretBody
@RestController
@RequestMapping("/repairCheck/repair")
public class SmsRepairCheckApplyController {
    @Autowired
    SmsRepairCheckApplyService smsRepairCheckApplyService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "新增维修报审申请")
    @PostMapping("/add")
    public ResponseResult repairCheck(@RequestBody SmsRepairCheckApplyVO applyVO,
                                      HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsRepairCheckApplyService.repairCheck(applyVO,user_id);
    }

    @ApiModelProperty(value = "撤销维修报审申请")
    @PostMapping("/cancel")
    public ResponseResult repairCheckCancel(@RequestBody SmsRepairCheckApplyCancelVO cancelVO,
                                            HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsRepairCheckApplyService.repairCheckCancel(cancelVO,user_id);
    }

    @ApiModelProperty(value = "审批")
    @PostMapping("/allow")
    public ResponseResult allowRepairCheck(@RequestBody SmsRepairCheckApplyAllowVO allowVO,
                                           HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsRepairCheckApplyService.allowRepairCheck(allowVO,user_id);
    }

    @ApiModelProperty("审批列表")
    @GetMapping("/list")
    public ResponseResult getRepairCheckList(@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                             @RequestParam(value = "search_str", required = false) String search_str,
                                             @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "apply_type") Integer apply_type,
                                           HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsRepairCheckApplyService.getRepairCheckList(pageSize,search_str,pageNum,apply_type,user_id);
    }

    @ApiModelProperty("审批详情")
    @GetMapping("/detail")
    public ResponseResult getRepairCheckDetail(@RequestParam(value = "apply_id") String apply_id,
                                             HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsRepairCheckApplyService.getRepairCheckDetail(apply_id,user_id);
    }

    @ApiModelProperty("申请人指定流程（申请前获取）")
    @GetMapping("/processList")
    public ResponseResult getRepairCheckOriginSettingProcess() {
        return smsRepairCheckApplyService.getRepairCheckOriginSettingProcess();
    }

    @ApiModelProperty(value = "查询所有申请记录")
    @GetMapping("/selectAll")
    public ResponseResult selectAllRepairCheck(
            @RequestParam(value = "apply_unit",required = false) String apply_unit,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "status",required = false) Integer status
    ){
        return smsRepairCheckApplyService.selectAllRepairCheck(apply_unit,
                pageSize,pageNum,status);
    }

    @ApiModelProperty(value = "文件下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam(value = "apply_id") String apply_id,
                                        HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsRepairCheckApplyService.tableDownload(apply_id,response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }
    @ApiOperation("批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportRepairCheck(@RequestBody SmsRepairCheckExportVO smsRepairCheckExportVO,
                                          HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsRepairCheckApplyService.exportRepairCheck(smsRepairCheckExportVO, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "强制归档")
    @PostMapping("/forced")
    public ResponseResult forcedFinishRepairCheck(@RequestBody SmsRepairCheckApplyForceVO forceVO,
                                                  HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsRepairCheckApplyService.forcedFinishRepairCheck(forceVO.getApplyId(), userId);
    }

    @ApiModelProperty(value = "强制流转")
    @PostMapping("/forcedTorsion")
    public ResponseResult forcedTorsionRepairCheck(@RequestBody SmsRepairCheckDetailTorsionVO torsionVO){
        return smsRepairCheckApplyService.forcedTorsionPayment(torsionVO);
    }

    @ApiModelProperty(value = "代审批")
    @PostMapping("/replaceApply")
    public ResponseResult replaceApplyRepairCheck(HttpServletRequest request,
                                                     @RequestBody SmsRepairCheckApplyAllowVO allowVO){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsRepairCheckApplyService.replaceApplyRepairCheck(userId,allowVO);
    }

    @ApiModelProperty(value = "强制驳回")
    @PostMapping("/forcedReject")
    public ResponseResult forcedRejectRepairCheck(@RequestBody SmsRepairCheckApplyForceVO forceVO,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsRepairCheckApplyService.forcedRejectRepairCheck(forceVO,userId);
    }
}

